package com.ldc.framework.adapter

import android.app.usage.StorageStatsManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonres.common.BaseRvAdapter
import com.ldc.commonres.common.BaseRvViewHolder
import com.ldc.framework.R
import com.ldc.framework.model.SystemModel

class SystemFragmentAdapter :
    BaseRvAdapter<SystemModel.SystemModelItem>(R.layout.framework_layout_view_item_system) {

    private var onRowListener: OnRowListener<SystemModel.SystemModelItem.Children>? = null

      fun addOnRowListener(onRowListener: OnRowListener<SystemModel.SystemModelItem.Children>) {
        this.onRowListener = onRowListener
    }

    override fun convertTo(
        holder: BaseRvViewHolder,
        data: SystemModel.SystemModelItem,
        position: Int
    ) {
        holder.setText(R.id.tv_title, data.name)
        initChildViews(holder, data)
    }

    private fun initChildViews(
        holder: BaseRvViewHolder,
        data: SystemModel.SystemModelItem
    ) {
        val adapter = SystemItemAdapter().also {
            it.addOnRowListener(this.onRowListener)
        }
        val rv = holder.findView(R.id.recycle_view) as RecyclerView
        val layoutManager = FlexboxLayoutManager(rv.context)
        layoutManager.justifyContent = JustifyContent.FLEX_START
        layoutManager.flexDirection = FlexDirection.ROW
        adapter.newData(data.children)
        rv.adapter = adapter
        rv.layoutManager = layoutManager
        rv.setHasFixedSize(true)
    }
}